package com.liu.service;

import com.liu.common.ParentTemplate;
import com.liu.common.Template;
import com.liu.utils.MyStringUtils;

/**
 * @author Mr.Liu
 * @version 1.0
 * @date 2024/8/31 11:36
 */
public class ServiceTemplate extends ParentTemplate implements Template {






    @Override
    public String toTemplate() {
        //Mapper变量名称
        String  SERVICE_VAR = MyStringUtils.toLowerFirstChar(this.getClassName());
        String GET_ID_FUNC  = "get"+ MyStringUtils.capitalizeFirstChar(this.getPrimaryKey())+"()";
        String TABLE_NAME = this.getTableName().toUpperCase();
        StringBuffer sb = new StringBuffer();
        sb.append("package "+this.getPackageName()+".service;\n\n");
        sb.append("import com.ais.framework.annotation.OperationLog;\n");
        sb.append("import com.ais.framework.common.CommonParent;\n");
        sb.append("import com.ais.framework.constant.Constants;\n");
        sb.append("import jakarta.annotation.Resource;\n");
        sb.append("import org.hibernate.service.spi.ServiceException;\n");
        sb.append("import org.springframework.stereotype.Service;\n");
        sb.append("import org.springframework.transaction.annotation.Transactional;\n");
        sb.append("import "+this.getPackageName()+".dao."+this.getClassName()+"Mapper\n");
        sb.append("import "+this.getPackageName()+".dao."+this.getClassName()+"Model\n");
        sb.append("import java.util.List;\n\n");

        sb.append("@Service\n");
        sb.append("public class "+this.getClassName()+"Service {\n");
        sb.append("    @Resource\n");
        sb.append("    private "+this.getClassName()+"Mapper "+SERVICE_VAR+"Mapper\n\n");


        //search start
        sb.append("    public List<"+this.getClassName()+"Model> search("+this.getClassName()+"Model model){\n");
        sb.append("        return "+SERVICE_VAR+"Mapper.search(model);\n");
        sb.append("    }\n\n");
        //search end

        //searchCount start
        sb.append("    public Object searchCount("+this.getClassName()+"Model model){\n");
        sb.append("        return "+SERVICE_VAR+"Mapper.searchCount(model);\n");
        sb.append("    }\n\n");
        //searchCount end

        //searchById start
        sb.append("    public "+this.getClassName()+"Model searchById(Integer "+this.getPrimaryKey()+"){\n");
        sb.append("        return "+SERVICE_VAR+"Mapper.searchById(model);\n");
        sb.append("    }\n\n");
        //searchById end


        //insert start
        sb.append("    @OperationLog(operation = Constants.ADD)\n");
        sb.append("    @Transactional(rollbackFor = Exception.class)\n");
        sb.append("    public Object insert("+this.getClassName()+"Model model){\n");
        sb.append("        model.setOperationContent(\""+SERVICE_VAR+"-insert-"+SERVICE_VAR+"-insert-ADD-"+TABLE_NAME+":\" + model.opeartionContent());\n");
        sb.append("        int result = "+SERVICE_VAR+"Mapper.insert(model);\n");
        sb.append("        if(result != 1) {\n" +
                "            throw new ServiceException(\"SQL_ERR_INSERT\");\n" +
                "        }\n");
        sb.append("        model.setOperationContent(\""+SERVICE_VAR+"-insert-"+SERVICE_VAR+"-insert-ADD-"+TABLE_NAME+":\" + model.opeartionContent());\n");
        sb.append("        return model."+GET_ID_FUNC+";\n");
        sb.append("    }\n\n");
        //insert end

        //update start
        sb.append("    @OperationLog(operation = Constants.MODIFY)\n");
        sb.append("    @Transactional(rollbackFor = Exception.class)\n");
        sb.append("    public Object update("+this.getClassName()+"Model model){\n");
        sb.append("        model.setOperationContent(\""+SERVICE_VAR+"-update-"+SERVICE_VAR+"-update-MODIFY-"+TABLE_NAME+":\" + model.opeartionContent());\n");
        sb.append("        Date lastupdateDate = "+SERVICE_VAR+"Mapper.getLastUpdateDate(model."+GET_ID_FUNC+");\n");
        sb.append("        if(lastupdateDate == null || !lastupdateDate.equals(model.getLastUpdatedDate())) {\n" +
                "            throw new ServiceException(\"DATA_UPDATE_TIMEOUT\");\n" +
                "        }\n");
        sb.append("        int result = "+SERVICE_VAR+"Mapper.update(model);\n");
        sb.append("        if(result != 1) {\n" +
                "              throw new ServiceException(\"SQL_ERR_INSERT\");\n" +
                "        }\n");
        sb.append("        return model."+GET_ID_FUNC+";\n");
        sb.append("    }\n\n");
        //update end

        //delete start
        sb.append("    @OperationLog(operation = Constants.DELETE)\n");
        sb.append("    @Transactional(rollbackFor = Exception.class)\n");
        sb.append("    public Object delete("+this.getClassName()+"Model model) {\n");
        sb.append("        model.setOperationContent(\""+SERVICE_VAR+"-delete-"+SERVICE_VAR+"-delete-DELETE-"+TABLE_NAME+":\" + model.opeartionContent());\n");
        sb.append("        int result = "+SERVICE_VAR+"Mapper.delete(model);\n");
        sb.append("        if(result != 1) {\n" +
                "           throw new ServiceException(\"SQL_ERR_DELETE\");\n" +
                "        }\n" +
                "        return result;\n");
        sb.append("    }\n");
        //delete end
        sb.append("}");

        return sb.toString();
    }
}
